package com.dunqi.gpm.shunqing.home.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.ComObserver;
import com.dunqi.gpm.shunqing.common.base.Data;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

public class EditSupervisoryManagementActivity extends BaseAct {

    private static final String SUPERVISERECORDID = "superviseRecordId";
    private static final String DATA = "data";
    private static final String CONTENT = "content";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.edit)
    EditText edit;
    private ProjectTimeline item;
    private long superviseRecordId;
    public static final int RESULT_CODE = 46341;
    private String content;


    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        Intent intent = getIntent();
        if (intent != null) {
            item = (ProjectTimeline) intent.getSerializableExtra(DATA);
            superviseRecordId = intent.getLongExtra(SUPERVISERECORDID, -1);
            content = intent.getStringExtra(CONTENT);
        }
        if (superviseRecordId != -1) {
            tip = "编辑";
            edit.setText(content);
            edit.setSelection(content.length());
        } else {
            tip = "新增";
        }
        titleBar.setTitle(tip + "督办");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_supervisory_management;
    }

    public String getContent() {
        return edit.getText().toString().trim();
    }


    String tip;

    @OnClick(R.id.config_tv)
    public void onViewClicked() {
        HashMap<String, String> hashMap = new HashMap<>();

        String content = getContent();

        if (TextUtils.isEmpty(content)) {
            t("请输入督办内容");
            return;
        }
        hashMap.put("projectId", String.valueOf(item.getProjectId()));
        hashMap.put("proAnnualPlanId", String.valueOf(item.getProAnnualPlanId()));
        hashMap.put("proPlanProgressId", String.valueOf(item.getProPlanProgressId()));
        hashMap.put("superviseContent", content);
        if (superviseRecordId != -1) {
            hashMap.put("superviseRecordId", String.valueOf(superviseRecordId));


        }


        final ProgressDialog dialog = ProgressDialog.show(this, "提示", "正在新增...");
        ApiFactory.getInstance()
                .saveProjectSupervise(hashMap)
                .compose(RxSchedulers.<Data>io_main())
                .subscribe(new ComObserver() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }

                    @Override
                    public void onSuccess(Object o) {
                        t(tip + "成功");
                        setResult(RESULT_CODE);
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        t(tip + "失败");
                    }

                    @Override
                    public void onFinal() {
                        dialog.dismiss();
                    }
                });
    }

    /**
     * @param context
     * @param projectTimeline
     * @param superviseRecordId -1 表示不存在
     */
    public static void start(Activity context, ProjectTimeline projectTimeline,
                             long superviseRecordId, String content) {
        Intent intent = new Intent(context, EditSupervisoryManagementActivity.class);
        intent.putExtra(SUPERVISERECORDID, superviseRecordId);
        intent.putExtra(DATA, projectTimeline);
        intent.putExtra(CONTENT, content);
        context.startActivityForResult(intent, 40);
    }


}
